//
//  FiveColumnsDetailViewController.m
//  SYiDaiy
//
//  Created by dllo on 16/9/2.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FiveColumnsDetailViewController.h"
#import "TAMacro.h"
#import "FiveDetailTableViewCell.h"
#import "AFNetworking.h"
#import "ColumnsViewController.h"


#import "Newspageesarray.h"

#import "Columnpageesarray.h"

#import "Viewpointesarray.h"

#import "Depthpageesarray.h"

#import "Activeesarray.h"
#import "FiveColumnsTwoDetailViewController.h"

@interface FiveColumnsDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *fiveDetailTableView;
//

@property (nonatomic,strong) NSMutableArray<Newspageesarray *> *newsfiveDetailArray;

//

@property (nonatomic,strong) NSMutableArray<Columnpageesarray *> *columnsfiveDetailArray;

//

@property (nonatomic,strong) NSMutableArray<Viewpointesarray *> *viewPointesarray;

//
;

@property (nonatomic,strong) NSMutableArray <Depthpageesarray *> *depthpageesArray;
//


@property (nonatomic,strong) NSMutableArray<Activeesarray *> *activeesArray;



@end

@implementation FiveColumnsDetailViewController

#pragma mark ---视图加载
- (void)viewDidLoad {
    [super viewDidLoad];


    [self createfiveDetailTableView];
//5个网络请求
    [self.fiveDetailTableView reloadData];
    [self newsPageNetworkGet];
    [self columnsPageNetworkGet];
    [self viewPointNetworkGet];
    [self depthPageNetworkGet];
    [self activeNetworkGet];
    
    
    
    
}

- (void)createfiveDetailTableView
{
    _fiveDetailTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 64) style:UITableViewStylePlain];
    _fiveDetailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    _fiveDetailTableView.delegate = self;
    _fiveDetailTableView.dataSource = self;
    
    [_fiveDetailTableView registerClass:[FiveDetailTableViewCell class] forCellReuseIdentifier:@"fiveDetailTableViewCell"];
    [self.view addSubview:_fiveDetailTableView];

}

#pragma mark -----新闻页网络请求
- (void)newsPageNetworkGet
{
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    NSString *requestURLString = @"http://watch-cdn.idailywatch.com/api/list/news/zh-hans?page=1&ver=iphone&app_ver=15";
    session.requestSerializer = [AFJSONRequestSerializer serializer];
    session.responseSerializer = [AFJSONResponseSerializer serializer];
    [session GET:requestURLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//       NSLog(@"%@",task.currentRequest);
//        NSLog(@"请求成功%@",responseObject);
        _newsfiveDetailArray = [NSMutableArray array];
        for (NSDictionary *dic in responseObject) {
          Newspageesarray *newspageesarray = [Newspageesarray modelWithDic:dic];
            [_newsfiveDetailArray addObject:newspageesarray];
        }
//        NSLog(@"-------%@",_newsfiveDetailArray[0].cover_thumb_hd);
        [self.fiveDetailTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       
    }];
}

#pragma mark ----专栏页网络请求
- (void)columnsPageNetworkGet
{
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    NSString *requestURLString = @"http://watch-cdn.idailywatch.com/api/list/column/zh-hans?page=1&ver=iphone&app_ver=15";
    session.requestSerializer = [AFJSONRequestSerializer serializer];
    session.responseSerializer = [AFJSONResponseSerializer serializer];
    [session GET:requestURLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//       NSLog(@"-------%@",responseObject);
        _columnsfiveDetailArray = [NSMutableArray array];
        for (NSDictionary *dic in responseObject) {
            Columnpageesarray *columnpageesarray = [Columnpageesarray modelWithDic:dic];
            [_columnsfiveDetailArray addObject:columnpageesarray];
        }
        NSLog(@"%@",_columnsfiveDetailArray[0].title);
        [self.fiveDetailTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

#pragma mark ----观点页网络请求
- (void)viewPointNetworkGet
{
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    NSString *requestURLString = @"http://watch-cdn.idailywatch.com/api/list/opinion/zh-hans?page=1&ver=iphone&app_ver=15";
    session.requestSerializer = [AFJSONRequestSerializer serializer];
    session.responseSerializer = [AFJSONResponseSerializer serializer];
    [session GET:requestURLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //       NSLog(@"-------%@",responseObject);
        _viewPointesarray = [NSMutableArray array];
        for (NSDictionary *dic in responseObject) {
            Viewpointesarray *viewpointesarray = [Viewpointesarray modelWithDic:dic];
            [_viewPointesarray addObject:viewpointesarray];
        }
        NSLog(@"%@",_viewPointesarray[0].title);
        [self.fiveDetailTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

}

#pragma mark ------深度页网络请求
- (void)depthPageNetworkGet
{
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    NSString *requestURLString = @"http://watch-cdn.idailywatch.com/api/list/feature/zh-hans?page=1&ver=iphone&app_ver=15";
    session.requestSerializer = [AFJSONRequestSerializer serializer];
    session.responseSerializer = [AFJSONResponseSerializer serializer];
    [session GET:requestURLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //       NSLog(@"-------%@",responseObject);
        _depthpageesArray = [NSMutableArray array];
        for (NSDictionary *dic in responseObject) {
            Depthpageesarray *depthpageesarray = [Depthpageesarray modelWithDic:dic];
            [_depthpageesArray addObject:depthpageesarray];
        }
        NSLog(@"%@",_depthpageesArray[0].title);
        [self.fiveDetailTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

}


#pragma mark ------活动页网络请求
- (void)activeNetworkGet
{
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    NSString *requestURLString = @"http://watch-cdn.idailywatch.com/api/list/events/zh-hans?page=1&ver=iphone&app_ver=15";
    session.requestSerializer = [AFJSONRequestSerializer serializer];
    session.responseSerializer = [AFJSONResponseSerializer serializer];
    [session GET:requestURLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //       NSLog(@"-------%@",responseObject);
        _activeesArray = [NSMutableArray array];
        for (NSDictionary *dic in responseObject) {
            Activeesarray *activeesarray = [Activeesarray modelWithDic:dic];
            [_activeesArray addObject:activeesarray];
        }
//        NSLog(@"%@",_depthpageesArray[0].title);
        [self.fiveDetailTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}



#pragma mark -----tableView的返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FiveDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"fiveDetailTableViewCell"];
    
    NSInteger row = [indexPath row];
    
    
    
    
    if (_rowValue == 0) {
        Newspageesarray *newspageesarray = [_newsfiveDetailArray objectAtIndex:row];
        NSString *title = newspageesarray.title;
        NSString *pubdate = newspageesarray.pubdate;
        NSString *author = newspageesarray.author;
        NSString *cover_thumb_hd = newspageesarray.cover_thumb_hd;
        
        cell.label1.text = title;
        cell.label2.text = pubdate;
        cell.label3.text = author;
       
        NSString *str = cover_thumb_hd;
        
        NSURL *url = [NSURL URLWithString:str];
        
        NSData *data = [NSData dataWithContentsOfURL:url];
        
        UIImage *image = [UIImage imageWithData:data];
        
        cell.imageView1.image = image;
        
        
    } else if (_rowValue == 1) {
        Columnpageesarray *columnpageesarray = [_columnsfiveDetailArray objectAtIndex:row];
        NSString *title = columnpageesarray.title;
        NSString *pubdate = columnpageesarray.pubdate;
        NSString *author = columnpageesarray.author;
        NSString *cover_thumb_hd = columnpageesarray.cover_thumb_hd;
        
        cell.label1.text = title;
        cell.label2.text = pubdate;
        cell.label3.text = author;
        
        NSString *str = cover_thumb_hd;
        
        NSURL *url = [NSURL URLWithString:str];
        
        NSData *data = [NSData dataWithContentsOfURL:url];
        
        UIImage *image = [UIImage imageWithData:data];
        
        cell.imageView1.image = image;
    } else if (_rowValue == 2) {
        Viewpointesarray *viewpointesarray = [_viewPointesarray objectAtIndex:row];
        NSString *title = viewpointesarray.title;
        NSString *pubdate = viewpointesarray.pubdate;
        NSString *author = viewpointesarray.author;
        NSString *cover_thumb_hd = viewpointesarray.cover_thumb_hd;
        
        cell.label1.text = title;
        cell.label2.text = pubdate;
        cell.label2.text = author;
        
        NSString *str = cover_thumb_hd;
        
        NSURL *url = [NSURL URLWithString:str];
        
        NSData *data = [NSData dataWithContentsOfURL:url];
        
        UIImage *image = [UIImage imageWithData:data];
        
        cell.imageView1.image = image;
    } else if (_rowValue == 3) {
        Depthpageesarray *depthpageesarray = [_depthpageesArray objectAtIndex:row];
        NSString *title = depthpageesarray.title;
        NSString *pubdate = depthpageesarray.pubdate;
        NSString *author = depthpageesarray.author;
        NSString *cover_thumb_hd = depthpageesarray.cover_thumb_hd;
        
        cell.label1.text = title;
        cell.label2.text = pubdate;
        cell.label2.text = author;
        
        NSString *str = cover_thumb_hd;
        
        NSURL *url = [NSURL URLWithString:str];
        
        NSData *data = [NSData dataWithContentsOfURL:url];
        
        UIImage *image = [UIImage imageWithData:data];
        
        cell.imageView1.image = image;
    } else if (_rowValue == 4) {
        Activeesarray *activeesarray = [_activeesArray objectAtIndex:row];
        NSString *title = activeesarray.title;
        NSString *pubdate = activeesarray.pubdate;
        NSString *author = activeesarray.author;
        NSString *cover_thumb_hd = activeesarray.cover_thumb_hd;
        
        cell.label1.text = title;
        cell.label2.text = pubdate;
        cell.label2.text = author;
        
        NSString *str = cover_thumb_hd;
        [cell.imageView1 sd_setImageWithURL:[NSURL URLWithString:str]];
    }
    return cell;
}


#pragma mark ----tableview的item的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_rowValue == 0) {
        NSInteger count = _newsfiveDetailArray.count;
        return count - 2;
    } else if (_rowValue == 1) {
        NSInteger count = _columnsfiveDetailArray.count;
        return count - 2;
    } else if (_rowValue == 2) {
        NSInteger count = _viewPointesarray.count;
        return count;
    } else if (_rowValue == 3) {
        NSInteger count = _depthpageesArray.count;
        return count - 2;
    } else  {
        NSInteger count = _activeesArray.count;
        return count;
    }
   
}

#pragma mark ----tableview的item的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}

#pragma mark ------tableView的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FiveColumnsTwoDetailViewController *fiveTwoVC = [FiveColumnsTwoDetailViewController new];
    if (_rowValue == 0) {
        fiveTwoVC.newsArray = _newsfiveDetailArray;
        fiveTwoVC.rowValue = indexPath.row;
    } else if (_rowValue == 1) {
        fiveTwoVC.columnsfiveDetailArray = _columnsfiveDetailArray;
        fiveTwoVC.rowValue = indexPath.row;
    } else if (_rowValue == 2) {
        fiveTwoVC.viewPointesarray = _viewPointesarray;
        fiveTwoVC.rowValue = indexPath.row;
    } else if (_rowValue == 3) {
        fiveTwoVC.depthpageesArray = _depthpageesArray;
        fiveTwoVC.rowValue = indexPath.row;
    } else {
        fiveTwoVC.activeesArray = _activeesArray;
        fiveTwoVC.rowValue = indexPath.row;
    }
    
    fiveTwoVC.row = _rowValue;
    [[self navigationController] pushViewController:fiveTwoVC animated:YES];
    
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

    
}
/*
#pragma mark - Navigation

 
 
 
 
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
